import net.sf.javaml.classification.Classifier;
import net.sf.javaml.classification.evaluation.CrossValidation;
import net.sf.javaml.classification.evaluation.PerformanceMeasure;

import java.util.Map;
import java.util.Random;

/**
 * Created by IntelliJ IDEA.
 * User: tobias
 * Date: 5/16/11
 * Time: 10:44 PM
 * To change this template use File | Settings | File Templates.
 */
public class Evaluate {

    public static void evaluate(Classifier classifier, DataSet dataSet) {
        CrossValidation cv = new CrossValidation(classifier);
        Map<Object, PerformanceMeasure> p = cv.crossValidation(dataSet,10, new Random(5));



        double tpr = 0;
        double fpr = 0;
        double precision = 0;
        double recall = 0;
        double fmesure = 0;

        double total = 0;
        double correct = 0;
        double incorrect = 0;
        for(Object o:p.keySet()) {
            correct = correct + p.get(o).tp;
            incorrect = incorrect + p.get(o).fp;

        }
        total = correct + incorrect;
        System.out.println("=== Summary === \n");
        System.out.println("Correctly classified instances:    " + Math.round(correct) +   "      " + String.format("%.3g",100*correct/total) + "  %");
        System.out.println("Incorrectly classified instances:  " + Math.round(incorrect) + "      " + String.format("%.3g",100*incorrect/total) + "  %");
        System.out.println("Total number of Instances :        " + Math.round(total) + "\n\n\n");

        System.out.println("=== Detailed Accuracy By Class ===\n");

        System.out.println("                TPR        FPR      PRECISION     RECALL    F-MEASURE      CLASS");
        System.out.println("                ------------------------------------------------------------------");
        for(Object o:p.keySet()) {
            double t = p.get(o).fp + p.get(o).tp;
            tpr = tpr + (t/total)*p.get(o).getTPRate();
            fpr = fpr + (t/total)*p.get(o).getFPRate();
            precision = precision + (t/total)*p.get(o).getPrecision();
            recall = recall + (t/total)*p.get(o).getRecall();
            fmesure = fmesure + (t/total)*p.get(o).getFMeasure();

            System.out.print("                ");
            System.out.print(String.format("%.3g", p.get(o).getTPRate()) + "      ");
            System.out.print(String.format("%.3g",p.get(o).getFPRate()) + "      ");
            System.out.print(String.format("%.3g",p.get(o).getPrecision()) + "      ");
            System.out.print(String.format("%.3g",p.get(o).getRecall()) + "      ");
            System.out.print(String.format("%.3g",p.get(o).getFMeasure()) + "      ");
            System.out.println(o);

        }
        System.out.print("\nweighted avg:   " + String.format("%.3g",tpr) + "      ");
        System.out.print(String.format("%.3g",fpr) + "      ");
        System.out.print(String.format("%.3g",precision) + "      ");
        System.out.print(String.format("%.3g",recall) + "      ");
        System.out.print(String.format("%.3g",fmesure) + "      ");


    }

}
